package org.iteam.xlz.core.service.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.user.ShareInfo;

public class ShareInfoService extends BaseService<ShareInfo>{
	static private ShareInfoService instance;
	
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;	
	private ShareInfoService(){}
	
	public static ShareInfoService getInstance(){
		if(null==instance){
			instance=new ShareInfoService();
		}
		return instance;
	}
	
	public long counts(String sharemobile,String targetmobile,String leftCreatetime,String rightCreatetime){
		params.clear();
		sql=new StringBuilder("select count(1) from shareinfo where 1=1");
		if(StringUtils.isNotBlank(sharemobile)){
			sql.append(" and sharemobile=?");
			params.add(sharemobile);
		}
		if(StringUtils.isNotBlank(targetmobile)){
			sql.append(" and targetmobile=?");
			params.add(targetmobile);
		}
		if(StringUtils.isNotBlank(leftCreatetime)&&StringUtils.isNotBlank(rightCreatetime)){
			sql.append(" and createtime between ? and ?");
			params.add(leftCreatetime);
			params.add(rightCreatetime);
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<ShareInfo> query(String sharemobile,String targetmobile,String leftCreatetime,String rightCreatetime,long page,long rows){
		params.clear();
		sql=new StringBuilder("select * from shareinfo where 1=1");
		if(StringUtils.isNotBlank(sharemobile)){
			sql.append(" and sharemobile=?");
			params.add(sharemobile);
		}
		if(StringUtils.isNotBlank(targetmobile)){
			sql.append(" and targetmobile=?");
			params.add(targetmobile);
		}
		if(StringUtils.isNotBlank(leftCreatetime)&&StringUtils.isNotBlank(rightCreatetime)){
			sql.append(" and createtime between ? and ?");
			params.add(leftCreatetime);
			params.add(rightCreatetime);
		}
		sql.append(" order by createtime desc limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ShareInfo>();
		}
	}
}
